package com.jrx.anytxn.param.service.aviator.impl;

import com.google.common.collect.Maps;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.param.entity.RmAviatorRuleSetType;
import com.jrx.anytxn.param.entity.RuleSelect;
import com.jrx.anytxn.param.mapper.ext.ExtRmAviatorRuleSetTypeMapper;
import com.jrx.anytxn.param.mapper.ext.ExtRuleSelectMapper;
import com.jrx.anytxn.param.service.aviator.IRuleResultService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
/**
 * 规则结果集的接口实现类
 * @author Qingyao Xing
 * @date 2019/10/10
 */
@Service
public class RuleResultServiceImpl implements IRuleResultService {

    @Resource
    private ExtRuleSelectMapper extRuleSelectMapper;
    @Resource
    private ExtRmAviatorRuleSetTypeMapper extRmAviatorRuleSetTypeMapper;

    @Override
    public TxnRespResult<HashMap<String, List<RuleSelect>>> getRuleMap(String tableName, String organizationId,String tenantId,String type) throws TxnException {
        List<RmAviatorRuleSetType> ruleSetTypes = extRmAviatorRuleSetTypeMapper.selectAll(tenantId);
        if (ruleSetTypes.isEmpty()) {
            throw new TxnException("规则集列表为空");
        } else {
            int count = 0;
            for (RmAviatorRuleSetType aviatorRuleSetType : ruleSetTypes) {
                if (aviatorRuleSetType.getRulesetTypeParaTable().equalsIgnoreCase(tableName)) {
                    count = 1;
                    break;
                }
            }
            if (count == 0) {
                throw new TxnException("不存在结果集为[" + tableName + "]表的规则集");
            }

        }
        TxnRespResult<HashMap<String, List<RuleSelect>>> resp = new TxnRespResult<>();
        HashMap<String, List<RuleSelect>> sysCodeMap = Maps.newHashMap();
        List<RuleSelect> list;
        list = extRuleSelectMapper.selectRuleSelectByTableName(tableName, organizationId,tenantId,type);
        sysCodeMap.put("listData", list);
        return resp.getSuccess(sysCodeMap);
    }
}
